package cn.lg.soar.system.biz.util;

import cn.lg.soar.common.algorithm.CryptoUtils;
import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.common.util.data.DataUtil;
import cn.lg.soar.common.util.data.RandomUtil;
import cn.lg.soar.common.util.listener.SpringUtils;

/**
 * @author luguoxiang 469599794@qq.com
 * @Date: 2024/6/1 13:17
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class FileAccessTokenUtils {

    private static String sign(String idsStr, String sale) {
        String secret = SpringUtils.getProperty("lg.general.upload.secret", "SntoNIroCEMHEYPBqWyUhJV0n4rzeNTS");
        return CryptoUtils.sm3Str((secret +"/"+ idsStr + "/" + sale).getBytes());
    }

    /**
     * 文件信息
     * @param ids
     * @return
     */
    public static String[] toFileInfo(String salt, long...ids) {
        String idsStr = DataUtil.longsToBase64(ids);
        String sign = sign(idsStr, salt);
        return new String[]{ sign, idsStr };
    }

    /**
     * 解析文件信息
     * @param sign
     * @param idsStr
     * @return
     */
    public static long[] parseFileInfo(String sale, String sign, String idsStr) {
        AssertUtil.equals(sign, sign(idsStr, sale), "链接无效");
        return DataUtil.base64ToLongs(idsStr);
    }

    /**
     * 创建临时访问路径
     * @param storageId
     * @param bucketName
     * @param filename
     * @param expire
     * @return
     */
    public static String createTempUrl(int storageId, String bucketName, String filename, long expire) {
        String[] split = filename.split("\\.");
        long fileId = Long.parseLong(split[0]);
        String[] strings = FileAccessTokenUtils.toFileInfo(
                "temp",
                fileId,
                RandomUtil.getLong(),
                System.currentTimeMillis() + expire
        );
        return String.format(
                "temp/%s/%s/%s.%s.%s",
                storageId,
                bucketName,
                strings[0],
                strings[1],
                split[1]
        );
    }

    public static String createAnonUrl(int storageId, String bucketName, String filename) {
        String[] split = filename.split("\\.");
        long fileId = Long.parseLong(split[0]);
        String[] strings = FileAccessTokenUtils.toFileInfo(
                "anon",
                fileId,
                RandomUtil.getLong(),
                System.currentTimeMillis()
        );
        return String.format(
                "anon/%s/%s/%s.%s.%s",
                storageId,
                bucketName,
                strings[0],
                strings[1],
                split[1]
        );
    }

    /**
     * 创建代理访问路径
     * @param storageId
     * @param bucketName
     * @param filename
     * @return
     */
    public static String createProxyUrl(int storageId, String bucketName, String filename) {
        return String.format(
                "proxy/%s/%s/%s",
                storageId,
                bucketName,
                filename
        );
    }

    /**
     * 创建重定向访问路径
     * @param storageId
     * @param bucketName
     * @param filename
     * @return
     */
    public static String createRedirectUrl(int storageId, String bucketName, String filename) {
        return String.format(
                "redirect/%s/%s/%s",
                storageId,
                bucketName,
                filename
        );
    }

    /**
     * 解析临时访问路径获取文件名
     * @param fileInfo
     * @return
     */
    public static String parseTempUrlFileInfo(String fileInfo) {
        String[] split = fileInfo.split("\\.");
        long[] longs = parseFileInfo("temp", split[0], split[1]);
        AssertUtil.isTrue(System.currentTimeMillis() < longs[2], "链接已失效");
        return longs[0] +"."+ split[2];
    }

    /**
     * 解析匿名访问路径获取文件名
     * @param fileInfo
     * @return
     */
    public static String parseAnonUrlFileInfo(String fileInfo) {
        String[] split = fileInfo.split("\\.");
        long[] longs = parseFileInfo("anon", split[0], split[1]);
        return longs[0] +"."+ split[2];
    }

}
